<?php
namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\Close as CloseModel;
use app\library\model\Area as AreaModel;

// 闭馆控制器
class Close extends Admin
{
    public function index(){    
    	$this->assign('tab_type', 3);
        if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $list = CloseModel::where($where)->page($page)->limit($limit)->order('id desc')->select();
            foreach ($list as $key => $value) {
                $list[$key]['starttime'] = date("Y-m-d H:i",$value['starttime']);
                $list[$key]['endtime'] = date("Y-m-d H:i",$value['endtime']);
                $list[$key]['count'] = 0;
            }
            $data['data'] = $list;
            $data['count'] = CloseModel::where($where)->count('id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * 添加闭馆时间
     * @return mixed
     */
    public function add(){
        
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            if ($data['starttime'] > $data['endtime']) {
                return $this->error('结束时间必须大于开始时间');
            }
            unset($data['id']);
            $areamodel = new AreaModel();
            $areaname = $areamodel->get_areaname($data['areaid']);
            $groupArr = $areamodel->get_child($data['areaid']);
            if (!empty($groupArr)) {
                $group = self::get_group_arr($groupArr);
                $group = $data['areaid'].$group;
            }else{
                $group = $data['areaid'];
            }
            $data['group'] = $group;
            $data['areaname'] = $areaname;
            $data['starttime'] = strtotime($data['starttime']);
            $data['endtime'] = strtotime($data['endtime']);
            if (!CloseModel::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }
        $id = $this->request->param('id');
        if (empty($id)) {
            $id = 0;
        }
        $this->assign('level_option', AreaModel::getAreaOption($id));
        return $this->fetch('form');
    }
    /**
     * 删除闭馆时间
     * @return mixed
     */
    public function del(){
        $id = input('param.id/a');
        $model = new CloseModel();
        if ($model->del($id)) {
            return $this->success('删除成功。');
        }
        return $this->error($model->getError());
    }
    /**
     * 修改闭馆时间
     * @return mixed
     */
    public function edit($id = 0){
        $row = CloseModel::where('id', $id)->field('id,areaid,areaname,starttime,endtime')->find();
        $row['starttime'] = date("Y-m-d H:i",$row['starttime']);
        $row['endtime'] = date("Y-m-d H:i",$row['endtime']);
        
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            if ($data['starttime'] > $data['endtime']) {
                return $this->error('结束时间必须大于开始时间');
            }
            $areamodel = new AreaModel();
            $areaname = $areamodel->get_areaname($data['areaid']);
            $groupArr = $areamodel->get_child($data['areaid']);
            if (!empty($groupArr)) {
                $group = self::get_group_arr($groupArr);
                $group = $data['areaid'].$group;
            }else{
                $group = $data['areaid'];
            }
            $data['group'] = $group;
            $data['areaname'] = $areaname;
            $data['starttime'] = strtotime($data['starttime']);
            $data['endtime'] = strtotime($data['endtime']);
            if (!CloseModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }
        $this->assign('data_info', $row);
        $this->assign('level_option', AreaModel::getAreaOption($row['areaid']));
        return $this->fetch('form');
    }
    /**
     * 把多维数组转为字符串id集
     * @return string
     */
    private function get_group_arr($group){
        $str = '';
        foreach ($group as $value) {
            $str .= ','.$value['id'];
            if (!empty($value['child'])) {
                $str .= self::get_group_arr($value['child']);
            }
        }
        return $str;
    }
}